const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current_index: 0,

    list: [],
    current_page: 1,
    last_page: 1,
    statusEnum: {
      1: '已兑换',
      2: '已转赠',
      3: '已过期',
      4: '已作废'
    }
  },
  behaviors: [skinBehavior],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      current_index: Number(options.status)
    })
    this.getData({
      page: 1,
      search: {
        status: options.status,
        current_index:options.status,
      }
    })
  },
  change(e) {
    let current_index = e.currentTarget.dataset.index
    this.setData({
      current_index
    })
    let status = current_index;
    this.getData({
      search: {
        status
      }
    });
  },
  getData(json) {
    app._postNetWork({
      url: app.getNetAddresss('plugin.gift-card.api.record.history'),
      data: json,
      success: resdata => {
        let {
          result,
          data: {
            data,
            current_page,
            last_page
          },
          msg
        } = resdata.data
        if (result) {
          this.setData({
            current_page,
            last_page
          })

          if (current_page == 1) {
            this.setData({
              list: data || []
            })
          } else {
            let arr = this.data.list.concat(data);
            this.setData({
              list: arr
            })
          }
        } else {
          app.tips(msg)
        }
      }
    })

  },
  getMoreData() {
    if (this.data.current_page >= this.data.last_page) {
      return;
    }
    let json = {
      page: this.data.current_page + 1,
      search: {
        status: this.data.active + 3
      }
    }
    this.getData(json)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMoreData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})